package com.li.controller;


import com.li.pojo.dto.UserDTO;
import com.li.pojo.dto.UserPageDTO;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.Result;
import com.li.pojo.po.User;
import com.li.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     *新增按钮
     */
    @GetMapping
    public Result getAdd(){
        return Result.success();
    }

    /**
     *根据id获取用户信息
     */
    @GetMapping("/{userId}")
    public Result getUserById(@PathVariable Integer userId){
        log.info("用户id：{}",userId);
        User user = userService.getById(userId);
        return Result.success(user);
    }

    /**
     *添加用户
     */
    @PostMapping
    public Result addUser(@RequestBody UserDTO userDTO){

        userService.addUser(userDTO);
        return Result.success();
    }

    /**
     *用户列表
     */
    @GetMapping("/list")
    public PageResult getUserList(UserPageDTO userPageDTO){
        return userService.getUserList(userPageDTO);
    }

    /**
     *修改信息
     */
    @PutMapping
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 修改状态
     */
    @PutMapping("/changeStatus")
    public Result updateStatus(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/{userIds}")
    public Result deleteById(@PathVariable String userIds){
        log.info(userIds);
        String[] ids = userIds.split(",");
        List<Integer> list = new ArrayList<>();
        for (String id : ids) {
            list.add(Integer.valueOf(id));
        }
        userService.removeByIds(list);
        return Result.success();
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public Result updatePassword(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }

    /**
     *获取用户下拉列表
     */
    @GetMapping("/listselect")
    public Result getList(Integer deptId){
        List<User> list = userService.getByDeptId(deptId);
        return Result.success(list);
    }
}
